package com.demo.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import com.demo.dao.RestaurantsRepository;
import com.demo.service.RestaurantsService;
import com.demo.utils.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.demo.domain.Restaurants;
import com.demo.exception.RestException;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

@Service
public class RestaurantsServiceImpl implements RestaurantsService {
	private Logger logger = LoggerFactory.getLogger(RestaurantsServiceImpl.class);
	
	@Autowired
	private RestaurantsRepository restaurantsRepository;
	
	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public Restaurants findById(String id) {
		if (StringUtils.isEmpty(id)) {
			throw new RestException(200,false,"id不能为空");
		}
		return restaurantsRepository.findOne(id);
	}

	@Override
	public List<Restaurants> findByName(String name) {
		if (StringUtils.isEmpty(name)) {
			throw new RestException(200,false,"name不能为空");
		}
		TimeUtils.startTime();
		logger.info("====getUser=============start ");
		List<Restaurants> list = restaurantsRepository.findByName(name);
		logger.info("====getUser============= end, time:"+TimeUtils.end());
		return list;
	}
	
	@Override
	public List<Restaurants> findAll() {
//        restaurantsRepository.findAll()
		return restaurantsRepository.findAll();
	}
	
	@Override
	public List<Restaurants> findAllPaging(int start, int count){
		Pageable pageable = new PageRequest(start, count, new Sort(Sort.Direction.ASC, "restaurant_id"));
		Page<Restaurants> pages = restaurantsRepository.findAll(pageable);
		Iterator<Restaurants> iterator = pages.iterator();
		List<Restaurants> list= new ArrayList<Restaurants>();
		while(iterator.hasNext()){
			list.add(iterator.next());
		}
		return list;
		
	}
	
	@Override
	public List<Restaurants> findByBorough(String borough) {
		if (StringUtils.isEmpty(borough)) {
			throw new RestException(200,false,"borough不能为空");
		}
		return restaurantsRepository.findByBorough(borough);
	}
	
	@Override
	public List<Restaurants> fingByCuisine(String cuisine) {
		if (StringUtils.isEmpty(cuisine)) {
			throw new RestException(200,false,"cuisine不能为空");
		}
		return restaurantsRepository.findByCuisine(cuisine);
	}
	
	@Override
	public Restaurants findByRestaurantId(String restaurantId) {
		if (StringUtils.isEmpty(restaurantId)) {
			throw new RestException(200,false,"restaurantId不能为空");
		}
		DBCollection dbCollection = mongoTemplate.getCollection("restaurants");
		DBObject query = new BasicDBObject("restaurant_id", restaurantId);
		
		DBObject object = dbCollection.find(query).one();
		if (object!=null) {
			Restaurants restaurants = new Restaurants();
		}
		
		return null;
	}
	
	
}
